<?php

//power by wshoto.com
global $_W, $_GPC;
$goods = array();
$openid = trim($_GPC['openid']);
$content = trim(urldecode($_GPC['content']));
if (empty($openid)) {
	die;
}
$member = m('member')->getMember($openid);
if (empty($member)) {
	die;
}
if (strexists($content, '+')) {
	$msg = explode('+', $content);
	$poster = pdo_fetch('select * from ' . tablename('ewei_shop_poster') . ' where keyword=:keyword and type=3 and isdefault=1 and uniacid=:uniacid limit 1', array(':keyword' => $msg[0], ':uniacid' => $_W['uniacid']));
	if (empty($poster)) {
		m('message')->sendCustomNotice($openid, '未找到商品海报类型!');
		die;
	}
	$goodsid = intval($msg[1]);
	if (empty($goodsid)) {
		m('message')->sendCustomNotice($openid, '未找到商品, 无法生成海报 !');
		die;
	}
} else {
	$poster = pdo_fetch('select * from ' . tablename('ewei_shop_poster') . ' where keyword=:keyword and isdefault=1 and uniacid=:uniacid limit 1', array(':keyword' => $content, ':uniacid' => $_W['uniacid']));
	if (empty($poster)) {
		m('message')->sendCustomNotice($openid, '未找到海报类型!');
		die;
	}
}
if ($member['isagent'] != 1 || $member['status'] != 1) {
	if (empty($poster['isopen'])) {
		$opentext = !empty($poster['opentext']) ? $poster['opentext'] : '您还不是我们分销商，去努力成为分销商，拥有你的专属海报吧!';
		m('message')->sendCustomNotice($openid, $opentext, trim($poster['openurl']));
		die;
	}
}
$waittext = !empty($poster['waittext']) ? $poster['waittext'] : '您的专属海报正在拼命生成中，请等待片刻...';
m('message')->sendCustomNotice($openid, $waittext);
$qr = $this->model->getQR($poster, $member, $goodsid);
if (is_error($qr)) {
	m('message')->sendCustomNotice($openid, '生成二维码出错: ' . $qr['message']);
	die;
}
$img = $this->model->createPoster($poster, $member, $qr);
$mediaid = $img['mediaid'];
$oktext = !empty($poster['oktext']) ? $poster['oktext'] : '您的专属海报已大功告成!';
if (!empty($mediaid)) {
	m('message')->sendCustomNotice($openid, $oktext);
	m('message')->sendImage($openid, $mediaid);
} else {
	$oktext .= "\n<a href='" . $img['img'] . "'>点击查看您的专属海报</a>";
	m('message')->sendCustomNotice($openid, $oktext);
}
die;